import math
import random

WIDTH =1350
HEIGHT = 700

BACK_COLOR = (0, 0, 127)
ADULT_COLOR = (0, 200, 0)
BABY_COLOR= (200,0,0)

def normalize(x, y):
    s = math.sqrt(x ** 2 + y ** 2)
    s+=1
    return x / s, y / s
class MyCircle:
    def __init__(self, x, y, r, dir_x, dir_y,ki):
        self.x = x
        self.y = y
        self.r = r
        self.dir_x, self.dir_y = normalize(dir_x, dir_y)
        self.spd = 1
        self.f_t=0
        self.ki_t=300
        self.ki=ki

    def SetSpeed(self, spd):
        self.spd = spd

    def Update(self):
        self.ki_t-=1
        if self.ki_t<=0:
            ki=1
        if self.f_t<1:
            self.x += self.dir_x * self.spd
            self.y += self.dir_y * self.spd
        else:
            self.x+=self.dir_x*(self.spd+1)
            self.y+=self.dir_y*(self.spd+1)
            self.f_t-=1
        bounce = False
        if self.x + self.r > WIDTH:
            self.x = WIDTH - self.r
            bounce = True

        if self.x - self.r < 0:
            self.x = self.r
            bounce = True

        if bounce == True:
            self.dir_x *= -1

        bounce = False

        if self.y + self.r > HEIGHT:
            self.y = HEIGHT - self.r
            bounce = True

        if self.y - self.r < 0:
            self.y = self.r
            bounce = True

        if bounce == True:
            self.dir_y *= -1

    def OnCollide(self, c):
        cdir_x = c.x - self.x
        cdir_y = c.y - self.y
        cdir_x, cdir_y = normalize(cdir_x, cdir_y)
        self.dir_x -= cdir_x
        self.dir_y -= cdir_y

        self.dir_x, self.dir_y = normalize(self.dir_x, self.dir_y)
        if self.f_t<=10:
            self.f_t+=(c.r+(c.spd-self.spd)+c.f_t//10)
    def Draw(self):
        if self.ki:
            screen.draw.filled_circle((self.x, self.y), self.r, ADULT_COLOR)
        else:
            screen.draw.filled_circle((self.x, self.y), self.r, BABY_COLOR)
Crl_list=[]
def on_key_down(key):
    if key == keys.ESCAPE:
        quit()

def on_mouse_down(pos):
    print("mouse_down", pos)
    crl=MyCircle(pos[0],pos[1],random.randint(3,10),random.randint(-5,5),random.randint(-5,5),1)
    crl.SetSpeed(random.randint(5,10))
    Crl_list.append(crl)

def on_mouse_up(pos):
    print("mouse_up", pos)
def SET():
    num=30
    for i in range(0,num):
        crl=MyCircle(random.randint(100,700),random.randint(100,400),random.randint(3,10),random.randint(-5,5),random.randint(-5,5),0)
        crl.SetSpeed(random.randint(7,10))
        Crl_list.append(crl)
SET()
'''
c1 = MyCircle(100, 100, 30, 3, 1)
c2 = MyCircle(150, 150, 30, 1, -2)
c3 = MyCircle(200, 200, 30, 1, 3)

c1.SetSpeed(2)
c2.SetSpeed(2)
c3.SetSpeed(2)
'''
def po(c1,c2,n):
    c1.x+=n
    c1.y-=n
    c2.x-=n
    c2.y+=n
def CheckCollision():
    global Crl_list
    for c1 in Crl_list:
        for c2 in Crl_list:
            if (not c1==c2) and math.sqrt((c1.x - c2.x) ** 2 + (c1.y - c2.y) ** 2) <= c1.r + c2.r:
                n=math.sqrt((c1.x - c2.x) ** 2 + (c1.y - c2.y) ** 2)
                po(c1,c2, (c1.r+c2.r-n)//2 )
                c1.OnCollide(c2)
                c2.OnCollide(c1)
                if random.randint(0,31)==30:
                    crl=MyCircle(c1.x,c2.y,(c1.r+c2.r)//3,random.randint(-5,5),random.randint(-5,5),1)
                    crl.SetSpeed(random.randint(5,10))
                    Crl_list.append(crl)
                elif random.randint(0,31)==1:
                    crl=MyCircle(c1.x,c2.y,(c1.r+c2.r),random.randint(-5,5),random.randint(-5,5),1)
                    crl.SetSpeed(random.randint(5,10))
                    Crl_list.append(crl)
                    if c1 in Crl_list and c2 in Crl_list:
                        Crl_list.remove(c1)
                        Crl_list.remove(c2)
def update():
    CheckCollision()
    for crl in Crl_list:
        crl.Update()
def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for crl in Crl_list:
        crl.Draw()